package Concurrency.basic.cooperation;

/**
 * 协作例子：同时开始
 */

public class FireFlag extends Thread{
    private volatile boolean fired = false;

    public synchronized void waitForFlag() throws InterruptedException {
        while (!fired) {
            wait();
        }
    }
    public synchronized void fire() {
        this.fired = true;
        notifyAll();
    }

    public static void main(String[] args) throws InterruptedException {
        int num = 10;
        FireFlag fireFlag = new FireFlag();
        Thread[] racers = new Thread[num];
        for (int i = 0; i < num; i++) {
            racers[i] = new Racer(fireFlag);
            racers[i].start();
        }
        Thread.sleep(1000);
        fireFlag.fire();
    }

}
